package com.xyy.saas.payment.cashier.core.vo;

import com.xyy.saas.payment.common.Builder;
import com.xyy.saas.payment.common.Constants;
import com.xyy.saas.payment.common.exception.PaymentException;
import com.xyy.saas.payment.cores.enums.ResultCodeEnum;
import com.xyy.saas.payment.payment.core.dto.PrepayDto;
import com.xyy.util.StringUtil;

import java.beans.Transient;
import java.io.Serializable;

public class CashierPrePayVO implements Serializable {

    private static final long serialVersionUID = -5309296297924660911L;
    /**
     * 支付渠道，比如使用ping++就传入ping++的key
     */

    private String paymentChannel;

    /**
     * 预支付的金额
     * 不能为空
     * 不能为非正数
     */
    	/** 预支付金额 */
    private Integer amount;

    /**
     * 不能为空、空串
     * 不能存在特殊字符
     */
    	/** 订单描述 */
    private String subject;

    /**
     * 不能为空、空串
     * 不能存在特殊字符
     */

    private String body;

    /**
     * 符合URL的规范
     */
    	/** 异步回调 */
    private String callback;
    /**
     * 商户的ID
     */
    	/** 商户id */
    private String businessId;
    /**
     * 由userId取代，保证扩展性
     */
    /** 收款方id */
    private String userId;

    /**
     * 业务订单号
     */
    	/** 业务订单账号 */
    private String businessOrderNo;

    /**
     * 业务类型 大业务线 saas 工多多等
     */
    	/** 业务类型 */
    private String businessType;

    /**
     * 支付渠道需要的额外参数
     */

    private String extra;

    	/** 业务调用接口标识 */
    private String invoker;

    /**
     * 业务id类型 大业务线下小业务ec_ka
     */
    	/** 业务id类型 */
    private String businessIdType;

    /**
     * 业务线订单类型
     */
    	/** 业务线订单类型 */
    private String businessOrderType;

    /**
     * 超时时间单位秒 1h -> 3600s
     */
    	/** 支付超时时间单位秒, ex:1h -> 3600s */
    private Long timeExpire;

    /**
     * 付款方id
     */
    /** 付款方id */
    private String payerId;

    /**
     * 付款方昵称
     */
    /** 付款方昵称 */
    private String payerName;

    /**
     * 收款方昵称
     */
    /** 收款方昵称 */
    private String sellerName;


    /**
     * 检验是否可以通过校验
     * @return true:有必传字段为空,不通过 false:通过
     */
    @Transient
    public void checked() throws PaymentException {
        if (StringUtil.isEmpty(paymentChannel, businessId, businessOrderNo, businessType, invoker, businessIdType, businessOrderType, callback)) {
            throw new PaymentException(ResultCodeEnum.PARAM_ERROR);
        }
        if (StringUtil.isEmpty(subject) || subject.length() > Constants.MAX_SUBJECT_LONG) {
            throw new PaymentException(ResultCodeEnum.PARAM_SUBJECT_ERROR);
        }
        if (StringUtil.isEmpty(body) || body.length() > Constants.MAX_BODY_LONG) {
            throw new PaymentException(ResultCodeEnum.PARAM_SUBJECT_ERROR);
        }
        if (amount == null || amount <= 0) {
            throw new PaymentException(ResultCodeEnum.PARAM_AMOUNT_ERROR);
        }
        if (timeExpire == null || timeExpire < Constants.MIN_TIME_EXPIRE || timeExpire > Constants.MAX_TIME_EXPIRE) {
            throw new PaymentException(ResultCodeEnum.PARAM_ERROR);
        }
    }

    @Transient
    public PrepayDto convert2PrepayDto() {
        return Builder.of(PrepayDto::new)
                .with(PrepayDto::setBusinessId, businessId)
                .with(PrepayDto::setBusinessOrderNo, businessOrderNo)
                .with(PrepayDto::setAmount, amount)
                .with(PrepayDto::setBusinessType, businessType)
                .with(PrepayDto::setBusinessIdType, businessIdType)
                .with(PrepayDto::setOrderType, businessOrderType)
                .with(PrepayDto::setBusinessOrderType, businessOrderType)
                .with(PrepayDto::setCallbackUrl, callback)
                .with(PrepayDto::setSubject, subject)
                .with(PrepayDto::setBody, body)
                .with(PrepayDto::setExtra, extra)
                .with(PrepayDto::setChannel, paymentChannel)
                .with(PrepayDto::setPayerId, payerId)
                .with(PrepayDto::setPayerName, payerName)
                .with(PrepayDto::setSellerName, sellerName)
                .build();

    }



    public String getPaymentChannel() {
        return paymentChannel;
    }

    public void setPaymentChannel(String paymentChannel) {
        this.paymentChannel = paymentChannel;
    }

    public Integer getAmount() {
        return amount;
    }

    public void setAmount(Integer amount) {
        this.amount = amount;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public String getCallback() {
        return callback;
    }

    public void setCallback(String callback) {
        this.callback = callback;
    }

    public String getBusinessId() {
        return businessId;
    }

    public void setBusinessId(String businessId) {
        this.businessId = businessId;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getBusinessOrderNo() {
        return businessOrderNo;
    }

    public void setBusinessOrderNo(String businessOrderNo) {
        this.businessOrderNo = businessOrderNo;
    }

    public String getBusinessType() {
        return businessType;
    }

    public void setBusinessType(String businessType) {
        this.businessType = businessType;
    }

    public String getExtra() {
        return extra;
    }

    public void setExtra(String extra) {
        this.extra = extra;
    }

    public String getBusinessIdType() {
        return businessIdType;
    }

    public void setBusinessIdType(String businessIdType) {
        this.businessIdType = businessIdType;
    }

    public String getBusinessOrderType() {
        return businessOrderType;
    }

    public void setBusinessOrderType(String businessOrderType) {
        this.businessOrderType = businessOrderType;
    }


    public String getInvoker() {
        return invoker;
    }

    public void setInvoker(String invoker) {
        this.invoker = invoker;
    }

    public Long getTimeExpire() {
        return timeExpire;
    }

    public void setTimeExpire(Long timeExpire) {
        this.timeExpire = timeExpire;
    }

    public String getPayerId() {
        return payerId;
    }

    public void setPayerId(String payerId) {
        this.payerId = payerId;
    }

    public String getPayerName() {
        return payerName;
    }

    public void setPayerName(String payerName) {
        this.payerName = payerName;
    }

    public String getSellerName() {
        return sellerName;
    }

    public void setSellerName(String sellerName) {
        this.sellerName = sellerName;
    }
}
